{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Available Captures for Rook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numRookCaptures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #可以被一步捕获的棋子数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在一个 8 x 8 的棋盘上，有一个白色的车（<code>Rook</code>），用字符 <code>&#39;R&#39;</code> 表示。棋盘上还可能存在空方块，白色的象（<code>Bishop</code>）以及黑色的卒（<code>pawn</code>），分别用字符 <code>&#39;.&#39;</code>，<code>&#39;B&#39;</code> 和 <code>&#39;p&#39;</code> 表示。不难看出，大写字符表示的是白棋，小写字符表示的是黑棋。</p>\n",
    "\n",
    "<p>车按国际象棋中的规则移动。东，西，南，北四个基本方向任选其一，然后一直向选定的方向移动，直到满足下列四个条件之一：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>棋手选择主动停下来。</li>\n",
    "\t<li>棋子因到达棋盘的边缘而停下。</li>\n",
    "\t<li>棋子移动到某一方格来捕获位于该方格上敌方（黑色）的卒，停在该方格内。</li>\n",
    "\t<li>车不能进入/越过已经放有其他友方棋子（白色的象）的方格，停在友方棋子前。</li>\n",
    "</ul>\n",
    "\n",
    "<p>你现在可以控制车移动一次，请你统计有多少敌方的卒处于你的捕获范围内（即，可以被一步捕获的棋子数）。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_1_improved.PNG\" style=\"height: 305px; width: 300px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：\n",
    "</strong>在本例中，车能够捕获所有的卒。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_2_improved.PNG\" style=\"height: 306px; width: 300px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;R&quot;,&quot;B&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;B&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：\n",
    "</strong>象阻止了车捕获任何卒。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/23/1253_example_3_improved.PNG\" style=\"height: 305px; width: 300px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>[[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;p&quot;,&quot;p&quot;,&quot;.&quot;,&quot;R&quot;,&quot;.&quot;,&quot;p&quot;,&quot;B&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;B&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;p&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释： </strong>\n",
    "车可以捕获位置 b5，d6 和 f5 的卒。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>board.length == board[i].length == 8</code></li>\n",
    "\t<li><code>board[i][j]</code> 可以是&nbsp;<code>&#39;R&#39;</code>，<code>&#39;.&#39;</code>，<code>&#39;B&#39;</code>&nbsp;或&nbsp;<code>&#39;p&#39;</code></li>\n",
    "\t<li>只有一个格子上存在&nbsp;<code>board[i][j] == &#39;R&#39;</code></li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [available-captures-for-rook](https://leetcode.cn/problems/available-captures-for-rook/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [available-captures-for-rook](https://leetcode.cn/problems/available-captures-for-rook/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"R\",\".\",\".\",\".\",\"p\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]', '[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"B\",\"R\",\"B\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]', '[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\"p\",\"p\",\".\",\"R\",\".\",\"p\",\"B\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"B\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    y, x = i, j\n",
    "                    break\n",
    "\n",
    "        r = ''.join(s for s in board[y] if s != '.')\n",
    "        c = ''.join(l[x] for l in board if l[x] != '.')\n",
    "\n",
    "        return r.count(\"Rp\") + r.count(\"pR\") + c.count(\"Rp\") + c.count(\"pR\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, boards: List[List[str]]) -> int:\n",
    "        dx = [1,-1,0,0]\n",
    "        dy = [0,0,1,-1]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if boards[i][j] == 'R':\n",
    "                    ans = 0\n",
    "                    for k in range(4):\n",
    "                        x, y = i, j\n",
    "                        while True:\n",
    "                            x += dx[k]\n",
    "                            y += dy[k]\n",
    "                            if (x < 0 or x >= 8) or (y < 0 or y >= 8) or boards[x][y] == 'B':\n",
    "                                break\n",
    "                            if boards[x][y] == 'p':\n",
    "                                ans += 1\n",
    "                                break\n",
    "                    # return ans\n",
    "        return  ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        RPos = None\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                ca = board[i][j]\n",
    "                if ca == 'R':\n",
    "                    RPos = (i, j)\n",
    "                    break\n",
    "        x, y = RPos\n",
    "        resultX = []\n",
    "        resultY = []\n",
    "        for col in range(y):\n",
    "            if board[x][col] == 'p':\n",
    "                resultX.append(col)\n",
    "            elif board[x][col] == 'B':\n",
    "                resultX.clear()\n",
    "        resultX = resultX[0:1] if len(resultX) > 1 else resultX\n",
    "        for col in range(y+1, 8):\n",
    "            if board[x][col] == 'p':\n",
    "                resultX.append(col)\n",
    "                break\n",
    "            elif board[x][col] == 'B':\n",
    "                break\n",
    "        for row in range(x):\n",
    "            if board[row][y] == 'p':\n",
    "                resultY.append(row)\n",
    "            elif board[row][y] == 'B':\n",
    "                resultY.clear()\n",
    "        resultY = resultY[0:1] if len(resultY) > 1 else resultY\n",
    "        for row in range(x+1, 8):\n",
    "            if board[row][y] == 'p':\n",
    "                resultY.append(row)\n",
    "                break\n",
    "            elif board[row][y] == 'B':\n",
    "                break\n",
    "        return len(resultX + resultY)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        # 找到R\n",
    "        def findR(board: List[List[str]]) -> List:\n",
    "            for x in range(8):\n",
    "                for y in range(8):\n",
    "                    if board[x][y] == 'R':\n",
    "                        return [x, y]\n",
    "\n",
    "        # 找到R的位置\n",
    "        pos = findR(board)\n",
    "        pr, pc = pos[0], pos[1]\n",
    "        count = 0\n",
    "        # 上\n",
    "        for i in range(pr - 1, -1, -1):\n",
    "            if board[i][pc] == 'p':\n",
    "                count += 1\n",
    "                break\n",
    "            elif board[i][pc] == 'B':\n",
    "                break\n",
    "        # 下\n",
    "        for i in range(pr + 1, 8):\n",
    "            if board[i][pc] == 'p':\n",
    "                count += 1\n",
    "                break\n",
    "            elif board[i][pc] == 'B':\n",
    "                break\n",
    "        # 左\n",
    "        for j in range(pc - 1, -1, -1):\n",
    "            if board[pr][j] == 'p':\n",
    "                count += 1\n",
    "                break\n",
    "            elif board[pr][j] == 'B':\n",
    "                break\n",
    "        # 右\n",
    "        for j in range(pc + 1, 8):\n",
    "            if board[pr][j] == 'p':\n",
    "                count += 1\n",
    "                break\n",
    "            elif board[pr][j] == 'B':\n",
    "                break\n",
    "        return count\n",
    "        # directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]\n",
    "        # for d in directions:\n",
    "        #     while\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        res = 0\n",
    "        m, n = len(board), len(board[0])\n",
    "        directs = [(-1, 0), (0, 1), (1, 0), (0, -1)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] != \"R\":\n",
    "                    continue\n",
    "\n",
    "                for x, y in directs:\n",
    "                    x1 = x + i\n",
    "                    y1 = y + j\n",
    "\n",
    "                    while 0 <= x1 < m and 0 <= y1 < n:\n",
    "                        if board[x1][y1] == \"B\":\n",
    "                            break\n",
    "\n",
    "                        if board[x1][y1] == \"p\":\n",
    "                            res += 1\n",
    "                            break\n",
    "\n",
    "                        x1 += x\n",
    "                        y1 += y\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        idx = 0\n",
    "        jdx = 0\n",
    "        for index, i in enumerate(board):\n",
    "            flag = False\n",
    "            for jndex, j  in enumerate(i):\n",
    "\n",
    "                if j == \"R\":\n",
    "                    flag = True\n",
    "                    idx = index\n",
    "                    jdx = jndex\n",
    "                    break\n",
    "            if flag:\n",
    "                break\n",
    "        res = 0\n",
    "        for i in range(idx):\n",
    "            if board[idx-i-1][jdx] == \"B\":\n",
    "                break\n",
    "            elif board[idx-i-1][jdx] == \"p\":\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        # print(\"aaaaaaaaaaaaaaaaa\")\n",
    "        for i in range(idx, len(board)):\n",
    "            print(idx, i)\n",
    "            # print(board[idx-i-1][jdx])\n",
    "            if board[i][jdx] == \"B\":\n",
    "                break\n",
    "            elif board[i][jdx] == \"p\":\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        # print(\"aaaaaaaaaaaaaaaaa\")\n",
    "        for j in range(jdx):\n",
    "            if board[idx][jdx-j-1] == \"B\":\n",
    "                break\n",
    "            elif board[idx][jdx-j-1] == \"p\":\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        for j in range(jdx, len(board)):\n",
    "            if board[idx][j] == \"B\":\n",
    "                break\n",
    "            elif board[idx][j] == \"p\":\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt = 0\n",
    "        st, ed = next((i, j) for i in range(8) for j in range(8) if board[i][j] == 'R')\n",
    "        dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]\n",
    "        for i in range(4):\n",
    "            step = 0\n",
    "            while True:\n",
    "                tx = st + step * dx[i]\n",
    "                ty = ed + step * dy[i]\n",
    "                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == 'B':\n",
    "                    break\n",
    "                if board[tx][ty] == 'p':\n",
    "                    cnt += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        Rx,Ry = 0,0\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[i])):\n",
    "                if board[i][j]=='R':#找到白车\n",
    "                    Rx,Ry = i,j\n",
    "        countl = 0\n",
    "        #从左边开始\n",
    "        for i in range(Ry):\n",
    "            if board[Rx][i]=='p':#黑卒\n",
    "                countl = 1\n",
    "                continue\n",
    "            if board[Rx][i]=='B':#白象\n",
    "                countl = 0\n",
    "        #从右边开始\n",
    "        countr = 0\n",
    "        for i in range(Ry+1,len(board[Rx])):\n",
    "            if board[Rx][i]=='B':#白象\n",
    "                break\n",
    "            if board[Rx][i]=='p':\n",
    "                countr = 1\n",
    "        #从上边开始\n",
    "        countu = 0\n",
    "        for j in range(Rx):\n",
    "            if board[j][Ry]=='p':\n",
    "                countu = 1\n",
    "                continue\n",
    "            if board[j][Ry]=='B':\n",
    "                countu = 0\n",
    "        #从下边开始\n",
    "        countd = 0\n",
    "        for j in range(Rx+1,len(board)):\n",
    "            if board[j][Ry]=='B':\n",
    "                break\n",
    "            if board[j][Ry]=='p':\n",
    "                countd = 1\n",
    "        return countu+countd+countl+countr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt, st, ed = 0, 0, 0\n",
    "        dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    st, ed = i, j\n",
    "        for i in range(4):\n",
    "            step = 0\n",
    "            while True:\n",
    "                tx = st + step * dx[i]\n",
    "                ty = ed + step * dy[i]\n",
    "                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == \"B\":\n",
    "                    break\n",
    "                if board[tx][ty] == \"p\":\n",
    "                    cnt += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt, st, ed = 0, 0, 0\n",
    "        dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    st, ed = i, j\n",
    "        for i in range(4):\n",
    "            step = 0\n",
    "            while True:\n",
    "                tx = st + step * dx[i]\n",
    "                ty = ed + step * dy[i]\n",
    "                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == \"B\":\n",
    "                    break\n",
    "                if board[tx][ty] == \"p\":\n",
    "                    cnt += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[0])):\n",
    "                if board[i][j] == \"R\":\n",
    "                    a = i\n",
    "                    b = j\n",
    "        res = 0\n",
    "        for k in range(b,-1,-1):\n",
    "            if board[a][k] == \"B\":\n",
    "                break\n",
    "            if board[a][k] == \"p\":\n",
    "                res = res + 1\n",
    "                break\n",
    "        for k in range(b,len(board[0])):\n",
    "            if board[a][k] == \"B\":\n",
    "                break\n",
    "            if board[a][k] == \"p\":\n",
    "                res = res + 1\n",
    "                break\n",
    "\n",
    "        for k in range(a,-1,-1):\n",
    "            if board[k][b] == \"B\":\n",
    "                break\n",
    "            if board[k][b] == \"p\":\n",
    "                res = res + 1\n",
    "                break\n",
    "\n",
    "        for k in range(a,len(board)):\n",
    "            if board[k][b] == \"B\":\n",
    "                break\n",
    "            if board[k][b] == \"p\":\n",
    "                res = res + 1\n",
    "                break\n",
    "                \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(len(board)):\n",
    "            if 'R' in board[i]:\n",
    "                row = i\n",
    "                break\n",
    "        colum = board[row].index('R')\n",
    "\n",
    "        result = 0 \n",
    "        s = ''.join(board[row])\n",
    "        s = s.replace('.','')\n",
    "        if 'pR' in s:\n",
    "            result += 1\n",
    "        if 'Rp' in s:\n",
    "            result += 1\n",
    "        s = ''.join(i[colum] for i in board)\n",
    "        s = s.replace('.','')\n",
    "        if 'pR' in s:\n",
    "            result += 1\n",
    "        if 'Rp' in s:\n",
    "            result += 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(len(board)):\n",
    "            if 'R' in board[i]:\n",
    "                raw = i\n",
    "                break\n",
    "        colum = board[raw].index('R')\n",
    "\n",
    "        r = ''.join(board[raw]).replace('.','')\n",
    "        c = ''.join(i[colum] for i in board).replace('.','')\n",
    "\n",
    "        return r.count(\"Rp\") + r.count(\"pR\") + c.count(\"Rp\") + c.count(\"pR\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:\n",
    "                        x, y = i+dx, j+dy\n",
    "                        while 0 <= x < 8 and 0 <= y < 8 and board[x][y] != 'B':\n",
    "                            if board[x][y] == 'p':\n",
    "                                ans += 1\n",
    "                                break\n",
    "                            x += dx\n",
    "                            y += dy\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        def judge(i,j,board):\n",
    "            m,n = len(board),len(board[0])\n",
    "            ans = 0\n",
    "            for l in range(i,-1,-1):\n",
    "                if board[i][l] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[i][l] == 'B':\n",
    "                    break\n",
    "            for r in range(i,n):\n",
    "                if board[i][r] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[i][r] == 'B':\n",
    "                    break\n",
    "            for u in range(j,-1,-1):\n",
    "                if board[u][j] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[u][j] == 'B':\n",
    "                    break\n",
    "            for d in range(j,n):\n",
    "                if board[d][j] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[d][j] == 'B':\n",
    "                    break\n",
    "            return ans\n",
    "        \n",
    "        r,c = len(board),len(board[0])\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if board[i][j] == 'R':\n",
    "                    return judge(i,j,board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ri,rj=0,0\n",
    "        count=0\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[0])):\n",
    "                if board[i][j]=='R':\n",
    "                    ri,rj=i,j\n",
    "                    break\n",
    "            if board[i][j]=='R':\n",
    "                break\n",
    "\n",
    "        for i in range(ri,-1,-1):\n",
    "            if board[i][rj]=='B':\n",
    "                break\n",
    "            elif board[i][rj]=='p':\n",
    "                count+=1\n",
    "                break\n",
    "        print(count)            \n",
    "        for i in range(ri,len(board)):\n",
    "            if board[i][rj]=='B':\n",
    "                break\n",
    "            elif board[i][rj]=='p':\n",
    "                count+=1\n",
    "                break \n",
    "        print(count)           \n",
    "        for j in range(rj,-1,-1):\n",
    "            if board[ri][j]=='B':\n",
    "                break\n",
    "            elif board[ri][j]=='p':\n",
    "                count+=1\n",
    "                break  \n",
    "        print(count)          \n",
    "        for j in range(rj,len(board[0])):\n",
    "            if board[ri][j]=='B':\n",
    "                break\n",
    "            elif board[ri][j]=='p':\n",
    "                count+=1\n",
    "                break            \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, boards: List[List[str]]) -> int:\n",
    "        dx = [1,-1,0,0]\n",
    "        dy = [0,0,1,-1]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if boards[i][j] == 'R':\n",
    "                    ans = 0\n",
    "                    for k in range(4):\n",
    "                        x, y = i, j\n",
    "                        while True:\n",
    "                            x += dx[k]\n",
    "                            y += dy[k]\n",
    "                            if (x < 0 or x >= 8) or (y < 0 or y >= 8) or boards[x][y] == 'B':\n",
    "                                break\n",
    "                            if boards[x][y] == 'p':\n",
    "                                ans += 1\n",
    "                                break\n",
    "                    return ans\n",
    "        return  ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j]=='R':\n",
    "                    a=i\n",
    "                    b=j\n",
    "                    break\n",
    "        sum=0\n",
    "        for i in range(b,-1,-1):\n",
    "            if board[a][i]==\"B\":\n",
    "                break\n",
    "            elif board[a][i]==\"p\":\n",
    "                sum+=1\n",
    "                break\n",
    "        for i in range(b,8):\n",
    "            if board[a][i]==\"B\":\n",
    "                break\n",
    "            elif board[a][i]==\"p\":\n",
    "                sum+=1\n",
    "                break\n",
    "        for i in range(a,-1,-1):\n",
    "            if board[i][b]==\"B\":\n",
    "                break\n",
    "            elif board[i][b]==\"p\":\n",
    "                sum+=1\n",
    "                break\n",
    "        for i in range(a,8):\n",
    "            if board[i][b]==\"B\":\n",
    "                break\n",
    "            elif board[i][b]==\"p\":\n",
    "                sum+=1\n",
    "                break     \n",
    "        return sum  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    y, x = i, j\n",
    "                    break\n",
    "\n",
    "        r = ''.join(board[y]).replace('.','')\n",
    "        c = ''.join(l[x] for l in board).replace('.','')\n",
    "\n",
    "        return r.count(\"Rp\") + r.count(\"pR\") + c.count(\"Rp\") + c.count(\"pR\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for ir in range(8):\n",
    "            for jr in range(8):\n",
    "                if board[ir][jr] == 'R':\n",
    "                    break\n",
    "            if board[ir][jr] == 'R':\n",
    "                break\n",
    "        ans = 0\n",
    "        for i in range(ir + 1,8):\n",
    "            if board[i][jr] != '.':\n",
    "                if board[i][jr] == 'p':\n",
    "                    ans += 1\n",
    "                break\n",
    "        for i in range(ir - 1,-1,-1):\n",
    "            if board[i][jr] != '.':\n",
    "                if board[i][jr] == 'p':\n",
    "                    ans += 1\n",
    "                break\n",
    "        for j in range(jr + 1,8):\n",
    "            if board[ir][j] != '.':\n",
    "                if board[ir][j] == 'p':\n",
    "                    ans += 1\n",
    "                break\n",
    "        for j in range(jr - 1,-1,-1):\n",
    "            if board[ir][j] != '.':\n",
    "                if board[ir][j] == 'p':\n",
    "                    ans += 1\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt,st,ed=0,0,0\n",
    "        dx,dy = [0,1,0,-1],[1,0,-1,0]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j]=='R':\n",
    "                    st,ed = i,j\n",
    "        for i in range(4):\n",
    "            step=0\n",
    "            while True:\n",
    "                tx = st + step*dx[i]\n",
    "                ty = ed + step*dy[i]\n",
    "                if tx<0 or tx>=8 or ty<0 or ty>=8 or board[tx][ty]==\"B\":\n",
    "                    break\n",
    "                if board[tx][ty] == \"p\":\n",
    "                    cnt+=1\n",
    "                    break\n",
    "                step+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def numRookCaptures(self, board):\n",
    "        return (lambda x:x('pR')+x('Rp'))([''.join(board[x]+[' ']+[i[y] for i in board]).replace('.','') for x in range(8) for y in range(8) if board[x][y]=='R'][0].count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        res=0\n",
    "        for i in range(len(board)):\n",
    "            if \"R\" in board[i]:\n",
    "                h=i\n",
    "                l=board[i].index(\"R\")\n",
    "        direct=[[1,0],[-1,0],[0,1],[0,-1]]\n",
    "        for d in direct:\n",
    "            i=h\n",
    "            j=l\n",
    "            while -1<i<len(board) and -1<j<len(board[0]):\n",
    "                if board[i][j]==\"p\":\n",
    "                    res+=1\n",
    "                    break\n",
    "                if board[i][j]==\"B\":\n",
    "                    break\n",
    "                i+=d[0]\n",
    "                j+=d[1]\n",
    "                \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    row, col = i, j\n",
    "        ans = 0\n",
    "        # right\n",
    "        for ind in range(col, 8):\n",
    "            if board[row][ind] == \"B\":\n",
    "                break\n",
    "            if board[row][ind] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "        # left\n",
    "        for ind in range(col, -1, -1):\n",
    "            if board[row][ind] == \"B\":\n",
    "                break\n",
    "            if board[row][ind] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "        board_t = list(zip(*board))\n",
    "        # down\n",
    "        for ind in range(row, 8):\n",
    "            if board_t[col][ind] == \"B\":\n",
    "                break\n",
    "            if board_t[col][ind] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "        # up\n",
    "        for ind in range(row, -1, -1):\n",
    "            if board_t[col][ind] == \"B\":\n",
    "                break\n",
    "            if board_t[col][ind] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\r\n",
    "        x, y, find = 0, 0, 0\r\n",
    "        for i in range(8):\r\n",
    "            for j in range(8):\r\n",
    "                if board[i][j] == 'R':\r\n",
    "                    x, y, find = i, j, 1\r\n",
    "                    break\r\n",
    "            if find: break\r\n",
    "        s = ''.join(board[x] + [' '] + [row[y] for row in board]).replace('.','')\r\n",
    "        return s.count('Rp') + s.count('pR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            if 'R' in board[i]:\n",
    "                j = board[i].index('R')\n",
    "                break\n",
    "        direction = ((0, 1), (1, 0), (0, -1), (-1, 0))\n",
    "        res = 0\n",
    "        for d in direction:\n",
    "            x, y = i, j\n",
    "            while x>-1 and x<8 and y>-1 and y<8:\n",
    "                if board[x][y]=='B':\n",
    "                    break\n",
    "                elif board[x][y]=='p':\n",
    "                    res+=1\n",
    "                    break\n",
    "                x, y = x+d[0], y+d[1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board) -> int:\n",
    "        row = len(board)\n",
    "        count = 0\n",
    "\n",
    "        def kill(i, j):\n",
    "            nonlocal board, count\n",
    "\n",
    "            if board[i][j] == 'p':\n",
    "                board[i][j] = 'e'\n",
    "                count += 1\n",
    "            if board[i][j] != \".\":\n",
    "                return False\n",
    "            return True\n",
    "\n",
    "        def Rook_checker(r, c):\n",
    "            nonlocal board\n",
    "            up = 1\n",
    "            while r - up >= 0:\n",
    "                if not kill(r - up, c):\n",
    "                    break\n",
    "                up += 1\n",
    "\n",
    "            down = 1\n",
    "            while r + down < row:\n",
    "                if not kill(r + down, c):\n",
    "                    break\n",
    "                down += 1\n",
    "\n",
    "            left = 1\n",
    "            while c - left >= 0:\n",
    "                if not kill(r, c - left):\n",
    "                    break\n",
    "                left += 1\n",
    "\n",
    "            right = 1\n",
    "            while c + right < row:\n",
    "                if not kill(r, c + right):\n",
    "                    break\n",
    "                right += 1\n",
    "\n",
    "\n",
    "\n",
    "        for i in range(row):\n",
    "            for j in range(row):\n",
    "                if board[i][j] == \"R\":\n",
    "                    Rook_checker(i, j)\n",
    "        return count\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        rowstring = ''\n",
    "        linestring = ''\n",
    "        m = 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R' or board[i][j] == 'p'or board[i][j] == 'B':\n",
    "                    rowstring += board[i][j]\n",
    "                if board[j][i] == 'R' or board[j][i] == 'p'or board[j][i] == 'B':\n",
    "                    linestring += board[j][i]\n",
    "            rowstring += ','\n",
    "            linestring += ','\n",
    "        print(rowstring)\n",
    "        print(linestring)\n",
    "        s1 = 'Rp'\n",
    "        s2 = 'pR'\n",
    "        if s1 in rowstring:\n",
    "            m += 1\n",
    "        if s2 in rowstring:\n",
    "            m += 1\n",
    "        if s1 in linestring:\n",
    "            m += 1\n",
    "        if s2 in linestring:\n",
    "            m += 1\n",
    "        \n",
    "\n",
    "        return m\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    for x in range(i-1, -1, -1):\n",
    "                        if board[x][j] == 'B':\n",
    "                            break\n",
    "                        elif board[x][j] == 'p':\n",
    "                            ans += 1\n",
    "                            break\n",
    "                    for x in range(i+1, 8):\n",
    "                        if board[x][j] == 'B':\n",
    "                            break\n",
    "                        elif board[x][j] == 'p':\n",
    "                            ans += 1\n",
    "                            break\n",
    "                    for y in range(j+1, 8):\n",
    "                        if board[i][y] == 'B':\n",
    "                            break\n",
    "                        elif board[i][y] == 'p':\n",
    "                            ans += 1\n",
    "                            break\n",
    "                    for y in range(j-1, -1, -1):\n",
    "                        if board[i][y] == 'B':\n",
    "                            break\n",
    "                        elif board[i][y] == 'p':\n",
    "                            ans += 1\n",
    "                            break\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    # North\n",
    "                    north = i\n",
    "                    while north >= 0 and board[north][j] != 'B' and board[north][j] != 'p':\n",
    "                        north -= 1\n",
    "                    if north >= 0 and north != i and board[north][j] == 'p':\n",
    "                        cnt += 1\n",
    "                    # South\n",
    "                    south = i\n",
    "                    while south < 8 and board[south][j] != 'B' and board[south][j] != 'p':\n",
    "                        south += 1\n",
    "                    if south < 8 and south != i and board[south][j] == 'p':\n",
    "                        cnt += 1\n",
    "                    # West\n",
    "                    west = j\n",
    "                    while west >= 0 and board[i][west] != 'B' and board[i][west] != 'p':\n",
    "                        west -= 1\n",
    "                    if west >= 0 and west != j and board[i][west] == 'p':\n",
    "                        cnt += 1\n",
    "                    # East\n",
    "                    east = j\n",
    "                    while east < 8 and board[i][east] != 'B' and board[i][east] != 'p':\n",
    "                        east += 1\n",
    "                    if east < 8 and east != j and board[i][east] == 'p':\n",
    "                        cnt += 1\n",
    "        return cnt\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n = len(board), len(board[0])\n",
    "        self.ans = 0\n",
    "        # visited = [[0] * n for _ in range(m)]\n",
    "        dirs = [(1,0), (-1,0), (0,1), (0,-1)]\n",
    "        def dfs(board, i, j, dx, dy):\n",
    "            if i < 0 or i >= m or j < 0 or j >= n:\n",
    "                return \n",
    "            if board[i][j] == 'B':\n",
    "                return\n",
    "            if board[i][j] == 'p':\n",
    "                self.ans += 1\n",
    "                return\n",
    "            if board[i][j] == '.':\n",
    "                i += dx\n",
    "                j += dy\n",
    "                dfs(board, i, j, dx, dy)\n",
    "             \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == 'R':\n",
    "                    for d in dirs:\n",
    "                        next_x = i + d[0]\n",
    "                        next_y = j + d[1]                \n",
    "                        dfs(board, next_x, next_y, d[0], d[1])\n",
    "                    # dfs(board, i, j)\n",
    "                    # dfs(board, i+1, j, 1, 0)\n",
    "                    # dfs(board, i-1, j, -1, 0)\n",
    "                    # dfs(board, i, j+1, 0, 1)\n",
    "                    # dfs(board, i, j-1, 0, -1)\n",
    "        return self.ans\n",
    "\n",
    "        # count = 0\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         if board[i][j] == 'R':\n",
    "        #             row = i\n",
    "        #             col = j\n",
    "        # for i in range(row-1, -1, -1):\n",
    "        #     if board[i][col] == 'B':\n",
    "        #         break\n",
    "        #     if board[i][col] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(row+1, m):\n",
    "        #     if board[i][col] == 'B':\n",
    "        #         break\n",
    "        #     if board[i][col] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(col-1, -1, -1):\n",
    "        #     if board[row][i] == 'B':\n",
    "        #         break\n",
    "        #     if board[row][i] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(col+1, n):\n",
    "        #     if board[row][i] == 'B':\n",
    "        #         break\n",
    "        #     if board[row][i] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n = len(board), len(board[0])\n",
    "        index = [[i, j] for i in range(m) for j in range(n) if board[i][j] == 'R'][0]\n",
    "        res  = 0 \n",
    "        direction = [[-1, 0], [1, 0], [0, -1], [0, 1]]\n",
    "        for i in range(4):\n",
    "            step = 0\n",
    "            while True:\n",
    "                nx, ny = index[0] + step * direction[i][0], index[1] + step * direction[i][1]\n",
    "                if nx >= m or nx < 0 or ny >= n or ny < 0 or board[nx][ny] == 'B':\n",
    "                    break\n",
    "                if board[nx][ny] == 'p':\n",
    "                    res += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt, st, ed = 0, 0, 0\n",
    "        dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    st, ed = i, j\n",
    "        for i in range(4):\n",
    "            step = 0\n",
    "            while True:\n",
    "                tx = st + step * dx[i]\n",
    "                ty = ed + step * dy[i]\n",
    "                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == \"B\":\n",
    "                    break\n",
    "                if board[tx][ty] == \"p\":\n",
    "                    cnt += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        row = 8\n",
    "        column = 8\n",
    "\n",
    "        for a in range(row):\n",
    "            for b in range(column):\n",
    "                if board[a][b] == \"R\":\n",
    "                    break\n",
    "\n",
    "            if board[a][b] == \"R\":\n",
    "                break\n",
    "        \n",
    "        count = 0\n",
    "        for i in range(a,row):\n",
    "            if board[i][b] == 'B':\n",
    "                break\n",
    "                \n",
    "            if board[i][b] == 'p':\n",
    "                count = count +1\n",
    "                break\n",
    "\n",
    "        print (\"step1\",count)\n",
    "        for i in range(a,-1,-1):\n",
    "            if board[i][b] == 'B':\n",
    "                break\n",
    "                \n",
    "            if board[i][b] == 'p':\n",
    "                count = count +1\n",
    "                break\n",
    "        \n",
    "        print (\"step2\",count)\n",
    "        \n",
    "        for j in range(b,-1,-1):\n",
    "            if board[a][j] == 'B':\n",
    "                break\n",
    "                \n",
    "            if board[a][j] == 'p':\n",
    "                count = count +1\n",
    "                break\n",
    "        \n",
    "        print (\"step3\",count)\n",
    "        for j in range(b,column):\n",
    "            if board[a][j] == 'B':\n",
    "                break\n",
    "                \n",
    "            if board[a][j] == 'p':\n",
    "                count = count +1\n",
    "                break\n",
    "\n",
    "        return count\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        def get_r():\n",
    "            for r_y in range(8):\n",
    "                for r_x in range(8):\n",
    "                    if board[r_y][r_x]=='R':\n",
    "                        return r_y,r_x\n",
    "\n",
    "        r_y,r_x=get_r()\n",
    "        result=0\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_y<7:\n",
    "            cur_y+=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_y>0:\n",
    "            cur_y-=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_x<7:\n",
    "            cur_x+=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_x>0:\n",
    "            cur_x-=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for  i in  range(8):\n",
    "            for j in range(8) :\n",
    "                if board[i][j]==\"R\":\n",
    "                    Rook_pos=[i,j]\n",
    "                    break\n",
    "        res=0\n",
    "        #right\n",
    "        for i in  range(Rook_pos[1]+1,8):\n",
    "            if board[Rook_pos[0]][i]==\".\":\n",
    "                continue\n",
    "            elif board[Rook_pos[0]][i]==\"B\":\n",
    "                break\n",
    "            elif board[Rook_pos[0]][i]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "        #left\n",
    "        for i in range(Rook_pos[1]-1,-1,-1):\n",
    "            if board[Rook_pos[0]][i]==\".\":\n",
    "                continue\n",
    "            elif board[Rook_pos[0]][i]==\"B\":\n",
    "                break\n",
    "            elif board[Rook_pos[0]][i]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "\n",
    "        #down\n",
    "        for i in  range(Rook_pos[0]+1,8):\n",
    "            if board[i][Rook_pos[1]]==\".\":\n",
    "                continue\n",
    "            elif board[i][Rook_pos[1]]==\"B\":\n",
    "                break\n",
    "            elif board[i][Rook_pos[1]]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "        #up\n",
    "        for i in range(Rook_pos[0]-1,-1,-1):\n",
    "            if board[i][Rook_pos[1]]==\".\":\n",
    "                continue\n",
    "            elif board[i][Rook_pos[1]]==\"B\":\n",
    "                break\n",
    "            elif board[i][Rook_pos[1]]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "    \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ans, r, c = 0, 0, 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    r, c = i, j\n",
    "                    break\n",
    "        direct = [(-1, 0), (0, 1), (1, 0), (0, -1)]\n",
    "        for d in direct:\n",
    "            nr, nc = r + d[0], c + d[1]\n",
    "            while 0 <= nr < 8 and 0 <= nc < 8:\n",
    "                if board[nr][nc] == \"B\":\n",
    "                    break\n",
    "                if board[nr][nc] == \"p\":\n",
    "                    ans += 1\n",
    "                    break\n",
    "                nr, nc = nr + d[0], nc + d[1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def find_r(self, board):\n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board[i])):\n",
    "                if board[i][j] == \"R\":\n",
    "                    return [i,j]\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        res = 0\n",
    "        i, j = self.find_r(board)\n",
    "        for idx in range(j+1, len(board)):\n",
    "            if board[i][idx] == 'B':\n",
    "                break\n",
    "            elif board[i][idx] == 'p':\n",
    "                res += 1\n",
    "                break\n",
    "        for idx in range(j-1, -1, -1): \n",
    "            if board[i][idx] == 'B':\n",
    "                break\n",
    "            elif board[i][idx] == 'p':\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        for idx in range(i-1, -1, -1):\n",
    "            if board[idx][j] == 'B':\n",
    "                break\n",
    "            elif board[idx][j] == 'p':\n",
    "                res += 1\n",
    "                break\n",
    "        for idx in range(i+1, len(board)):\n",
    "            if board[idx][j] == 'B':\n",
    "                break\n",
    "            elif board[idx][j] == 'p':\n",
    "                res += 1\n",
    "                break\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(board)):\n",
    "            if 'R' in board[i]:\n",
    "                x,y=board[i].index('R'),i\n",
    "                break\n",
    "        t=board[y]\n",
    "        t1=list(zip(*board))[x]\n",
    "        for i in range(y+1,8):\n",
    "            if t1[i]!='.':\n",
    "                ans+=t1[i]=='p'\n",
    "                break\n",
    "        for i in range(y-1,-1,-1):\n",
    "            if t1[i]!='.':\n",
    "                ans+=t1[i]=='p'\n",
    "                break\n",
    "        for i in range(x+1,8):\n",
    "            if t[i]!='.':\n",
    "                ans+=t[i]=='p'\n",
    "                break\n",
    "        for i in range(x-1,-1,-1):\n",
    "            if t[i]!='.':\n",
    "                ans+=t[i]=='p'\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, boards: List[List[str]]) -> int:\n",
    "        dx = [1,0,-1,0]\n",
    "        dy = [0,1,0,-1]\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if boards[i][j] == 'R':\n",
    "                    ans = 0\n",
    "                    for k in range(4):\n",
    "                        x, y = i, j\n",
    "                        while True:\n",
    "                            x += dx[k]\n",
    "                            y += dy[k]\n",
    "                            if (x < 0 or x >= 8) or (y < 0 or y >= 8) or boards[x][y] == 'B':\n",
    "                                break\n",
    "                            if boards[x][y] == 'p':\n",
    "                                ans += 1\n",
    "                                break\n",
    "                    # return ans\n",
    "        return  ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        def get_r():\n",
    "            for r_y in range(8):\n",
    "                for r_x in range(8):\n",
    "                    if board[r_y][r_x]=='R':\n",
    "                        return r_y,r_x\n",
    "\n",
    "        r_y,r_x=get_r()\n",
    "        result=0\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_y<7:\n",
    "            cur_y+=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_y>0:\n",
    "            cur_y-=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_x<7:\n",
    "            cur_x+=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        cur_y,cur_x=r_y,r_x\n",
    "        while cur_x>0:\n",
    "            cur_x-=1\n",
    "            if board[cur_y][cur_x]=='p':\n",
    "                result+=1\n",
    "                break\n",
    "            elif board[cur_y][cur_x]=='B':\n",
    "                break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ret = 0\n",
    "        for i, l in enumerate(board):\n",
    "            for j, c in enumerate(l):\n",
    "                if c == 'R':\n",
    "                    ti = i\n",
    "                    while ti > 0:\n",
    "                        ti -= 1\n",
    "                        if board[ti][j] == 'p':\n",
    "                            ret += 1\n",
    "                            break\n",
    "                        if board[ti][j] == 'B':\n",
    "                            break\n",
    "                    ti = i\n",
    "                    while ti < 8-1:\n",
    "                        ti += 1\n",
    "                        if board[ti][j] == 'p':\n",
    "                            ret += 1\n",
    "                            break\n",
    "                        if board[ti][j] == 'B':\n",
    "                            break\n",
    "                    tj = j\n",
    "                    while tj > 0:\n",
    "                        tj -= 1\n",
    "                        if board[i][tj] == 'p':\n",
    "                            ret += 1\n",
    "                            break\n",
    "                        if board[i][tj] == 'B':\n",
    "                            break\n",
    "                    tj = j\n",
    "                    while tj < 8-1:\n",
    "                        tj += 1\n",
    "                        if board[i][tj] == 'p':\n",
    "                            ret += 1\n",
    "                            break\n",
    "                        if board[i][tj] == 'B':\n",
    "                            break\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n = len(board), len(board[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == 'R':\n",
    "                    index = [i, j]\n",
    "        res = 0\n",
    "        for i, j in [[-1, 0], [1, 0], [0, -1], [0, 1]]:\n",
    "            step = 0\n",
    "            while True:\n",
    "                nr, nc = index[0] + step * i, index[1] + step * j\n",
    "                if not 0 <= nr < m or not 0 <= nc < n or board[nr][nc] == 'B':\n",
    "                    break\n",
    "                if board[nr][nc] == 'p':\n",
    "                    res += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n = len(board), len(board[0])\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == 'R':\n",
    "                    for x, y in (-1,0),(1,0),(0,-1),(0,1):\n",
    "                        r, c = i + x, j + y\n",
    "                        while 0<=r<m and 0<=c<n and board[r][c] == '.':\n",
    "                            r += x \n",
    "                            c += y\n",
    "                        if 0<=r<m and 0<=c<n and board[r][c] == 'p':\n",
    "                            ans += 1\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        x,y=0,0\n",
    "        for i in range(8):\n",
    "            if 'R' in board[i]:\n",
    "                x=i\n",
    "                y=board[i].index('R')\n",
    "                break\n",
    "        c=0\n",
    "        for i in range(x-1,-1,-1):\n",
    "            if board[i][y]=='.':\n",
    "                continue\n",
    "            elif board[i][y]!='p':\n",
    "                break\n",
    "            elif board[i][y]=='p':\n",
    "                c+=1\n",
    "                break\n",
    "        for i in range(x+1,8,1):\n",
    "            if board[i][y]=='.':\n",
    "                continue\n",
    "            elif board[i][y]!='p':\n",
    "                break\n",
    "            elif board[i][y]=='p':\n",
    "                c+=1\n",
    "                break\n",
    "        for j in range(y-1,-1,-1):\n",
    "            if board[x][j]=='.':\n",
    "                continue\n",
    "            elif board[x][j]!='p':\n",
    "                break\n",
    "            elif board[x][j]=='p':\n",
    "                c+=1\n",
    "                break\n",
    "        for j in range(y+1,8,1):\n",
    "            if board[x][j]=='.':\n",
    "                continue\n",
    "            elif board[x][j]!='p':\n",
    "                break\n",
    "            elif board[x][j]=='p':\n",
    "                c+=1    \n",
    "                break\n",
    "        return c    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for r, row in enumerate(board):\n",
    "            for c, cell in enumerate(row):\n",
    "                if cell == 'R':\n",
    "                    ans = 0\n",
    "                    for i in range(c+1, 8):\n",
    "                        if row[i] != '.':\n",
    "                            if row[i] == 'p':\n",
    "                                ans += 1\n",
    "                            break\n",
    "                    for i in range(c-1, -1, -1):\n",
    "                        if row[i] != '.':\n",
    "                            if row[i] == 'p':\n",
    "                                ans += 1\n",
    "                            break\n",
    "                    for i in range(r+1, 8):\n",
    "                        if board[i][c] != '.':\n",
    "                            if board[i][c] == 'p':\n",
    "                                ans += 1\n",
    "                            break\n",
    "                    for i in range(r-1, -1, -1):\n",
    "                        if board[i][c] != '.':\n",
    "                            if board[i][c] == 'p':\n",
    "                                ans += 1\n",
    "                            break\n",
    "\n",
    "                    return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        result = 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    for m in range(i, -1, -1):\n",
    "                        if board[m][j] == 'p':\n",
    "                            result += 1\n",
    "                            break\n",
    "                        if board[m][j] == 'B':\n",
    "                            break\n",
    "                    for m in range(i, 8):\n",
    "                        if board[m][j] == 'p':\n",
    "                            result += 1\n",
    "                            break\n",
    "                        if board[m][j] == 'B':\n",
    "                            break\n",
    "                    for n in range(j, -1, -1):\n",
    "                        if board[i][n] == 'p':\n",
    "                            result += 1\n",
    "                            break\n",
    "                        if board[i][n] == 'B':\n",
    "                            break\n",
    "                    for n in range(j, 8):\n",
    "                        if board[i][n] == 'p':\n",
    "                            result += 1\n",
    "                            break\n",
    "                        if board[i][n] == 'B':\n",
    "                            break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        row, col = -1, -1\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    row, col = i, j\n",
    "                    break\n",
    "        ans = 0\n",
    "        # 左右\n",
    "        for i in range(col, -1, -1):\n",
    "            if board[row][i] == 'B':\n",
    "                break\n",
    "            if board[row][i] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "        # print(\"ans=\", ans)\n",
    "        for i in range(col+1, 8, 1):\n",
    "            if board[row][i] == 'B':\n",
    "                break\n",
    "            if board[row][i] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "        # print(\"ans=\", ans)\n",
    "        # 上下\n",
    "        for i in range(row, -1, -1):\n",
    "            if board[i][col] == 'B':\n",
    "                break\n",
    "            if board[i][col] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "        # print(\"ans=\", ans)\n",
    "        for i in range(row+1, 8, 1):\n",
    "            if board[i][col] == 'B':\n",
    "                break\n",
    "            if board[i][col] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "        # print(\"ans=\", ans)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(len(board)):\n",
    "            if \"R\" in board[i]:\n",
    "                j = board[i].index(\"R\")\n",
    "                break\n",
    "        ans = 0\n",
    "        x_1, x_2, y_1, y_2 = j-1, j+1, i-1, i+1\n",
    "        while x_1 >= 0:\n",
    "            if board[i][x_1] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "            elif board[i][x_1] == \"B\":\n",
    "                break\n",
    "            else:\n",
    "                x_1 -= 1\n",
    "        while x_2 < 8:\n",
    "            if board[i][x_2] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "            elif board[i][x_2] == \"B\":\n",
    "                break\n",
    "            else:\n",
    "                x_2 += 1\n",
    "        while y_1 >= 0:\n",
    "            if board[y_1][j] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "            elif board[y_1][j] == \"B\":\n",
    "                break\n",
    "            else:\n",
    "                y_1 -= 1\n",
    "        while y_2 < 8:\n",
    "            if board[y_2][j] == \"p\":\n",
    "                ans += 1\n",
    "                break\n",
    "            elif board[y_2][j] == \"B\":\n",
    "                break\n",
    "            else:\n",
    "                y_2 += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        def judge(i,j,board):\n",
    "            m,n = len(board),len(board[0])\n",
    "            ans = 0\n",
    "            for l in range(i,-1,-1):\n",
    "                if board[i][l] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[i][l] == 'B':\n",
    "                    break\n",
    "            for r in range(i,n):\n",
    "                if board[i][r] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[i][r] == 'B':\n",
    "                    break\n",
    "            for u in range(j,-1,-1):\n",
    "                if board[u][j] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[u][j] == 'B':\n",
    "                    break\n",
    "            for d in range(j,n):\n",
    "                if board[d][j] == 'p':\n",
    "                    ans += 1\n",
    "                    break\n",
    "                elif board[d][j] == 'B':\n",
    "                    break\n",
    "            return ans\n",
    "        \n",
    "        r,c = len(board),len(board[0])\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if board[i][j] == 'R':\n",
    "                    return judge(i,j,board)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        isfind = 0\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    x,y = i,j\n",
    "                    isfind = 1\n",
    "                    break\n",
    "            \n",
    "            if isfind:break\n",
    "\n",
    "        cnt = 0\n",
    "        i = y-1\n",
    "        while 0<= i < y:\n",
    "            if board[x][i] == 'B':break           \n",
    "            if board[x][i] == 'p':\n",
    "                cnt += 1\n",
    "                break\n",
    "            if board[x][i] == '.':i -= 1\n",
    "        i = y + 1\n",
    "        while y < i < 8:\n",
    "            if board[x][i] == 'B':break     \n",
    "            if board[x][i] == 'p':\n",
    "                cnt += 1\n",
    "                break \n",
    "            if board[x][i] == '.':i += 1\n",
    "        i = x + 1\n",
    "        while x < i < 8:\n",
    "            if board[i][y] == 'B':break\n",
    "            \n",
    "            if board[i][y] == 'p':\n",
    "                cnt += 1\n",
    "                break \n",
    "            if board[i][y] == '.':i += 1\n",
    "        i = x - 1\n",
    "        while 0 <= i < x:\n",
    "            if board[i][y] == 'B':break\n",
    "           \n",
    "            if board[i][y] == 'p':\n",
    "                cnt += 1\n",
    "                break \n",
    "            if board[i][y] == '.':i -= 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\r\n",
    "        def catch(x, y, range, where):\r\n",
    "            if where == 'col':\r\n",
    "                for i in range:\r\n",
    "                    if board[i][y] == '.':\r\n",
    "                        continue\r\n",
    "                    elif board[i][y] == 'p':\r\n",
    "                        return 1\r\n",
    "                    return 0\r\n",
    "            if where == 'row':\r\n",
    "                for j in range:\r\n",
    "                    if board[x][j] == '.':\r\n",
    "                        continue\r\n",
    "                    elif board[x][j] == 'p':\r\n",
    "                        return 1\r\n",
    "                    return 0\r\n",
    "            return 0\r\n",
    "        x, y, find = 0, 0, 0\r\n",
    "        for i in range(8):\r\n",
    "            for j in range(8):\r\n",
    "                if board[i][j] == 'R':\r\n",
    "                    x, y, find = i, j, 1\r\n",
    "                    break\r\n",
    "            if find: break\r\n",
    "        cnt = catch(x, y, range(x-1,-1,-1), 'col') + catch(x, y, range(x+1, 8), 'col') + \\\r\n",
    "        catch(x, y, range(y-1,-1,-1), 'row') + catch(x, y, range(y+1, 8), 'row')\r\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        x,y = None,None\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j]=='R':\n",
    "                    x,y = i,j\n",
    "                    break\n",
    "            if not x is None:\n",
    "                break\n",
    "        ans = 0\n",
    "        for i in range(x-1,-1,-1):\n",
    "            if board[i][y]=='B':\n",
    "                break\n",
    "            if board[i][y]=='p':\n",
    "                ans += 1\n",
    "                break\n",
    "        for i in range(x+1,8):\n",
    "            if board[i][y]=='B':\n",
    "                break\n",
    "            if board[i][y]=='p':\n",
    "                ans += 1\n",
    "                break\n",
    "        for i in range(y-1,-1,-1):\n",
    "            if board[x][i]=='B':\n",
    "                break\n",
    "            if board[x][i]=='p':\n",
    "                ans += 1\n",
    "                break\n",
    "        for i in range(y+1,8):\n",
    "            if board[x][i]=='B':\n",
    "                break\n",
    "            if board[x][i]=='p':\n",
    "                ans += 1\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        revboard = zip(*board)\n",
    "        count = 0\n",
    "        for j in board:\n",
    "            if \"R\" in j:\n",
    "                index = j.index(\"R\")\n",
    "                print(index)\n",
    "                l = 1\n",
    "                r = 1\n",
    "                while True:\n",
    "                    if index-l<0:\n",
    "                        break\n",
    "                    elif j[index-l] == \"p\":\n",
    "                        count += 1\n",
    "                        break\n",
    "                    elif j[index-l] == \"B\":\n",
    "                        break\n",
    "                    l += 1\n",
    "                while True:\n",
    "                    if index+r>7:\n",
    "                        break\n",
    "                    elif j[index+r] == \"p\":\n",
    "                        count+=1\n",
    "                        break\n",
    "                    elif j[index+r] == \"B\":\n",
    "                        break\n",
    "                    r += 1\n",
    "        for i in revboard:\n",
    "            if \"R\" in i:\n",
    "                index = i.index(\"R\")\n",
    "                print(index)\n",
    "                l = 1\n",
    "                r = 1\n",
    "                while True:\n",
    "                    if index-l<0:\n",
    "                        break\n",
    "                    elif i[index-l] == \"p\":\n",
    "                        count += 1\n",
    "                        break\n",
    "                    elif i[index-l] == \"B\":\n",
    "                        break\n",
    "                    l += 1\n",
    "                while True:\n",
    "                    if index+r>7:\n",
    "                        break\n",
    "                    elif i[index+r] == \"p\":\n",
    "                        count+=1\n",
    "                        break\n",
    "                    elif i[index+r] == \"B\":\n",
    "                        break\n",
    "                    r += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for  i in  range(8):\n",
    "            for j in range(8) :\n",
    "                if board[i][j]==\"R\":\n",
    "                    Rook_pos=[i,j]\n",
    "                    break\n",
    "        res=0\n",
    "        #right\n",
    "        for i in  range(Rook_pos[1]+1,8):\n",
    "            if board[Rook_pos[0]][i]==\".\":\n",
    "                continue\n",
    "            elif board[Rook_pos[0]][i]==\"B\":\n",
    "                break\n",
    "            elif board[Rook_pos[0]][i]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "        #left\n",
    "        for i in range(Rook_pos[1]-1,-1,-1):\n",
    "            if board[Rook_pos[0]][i]==\".\":\n",
    "                continue\n",
    "            elif board[Rook_pos[0]][i]==\"B\":\n",
    "                break\n",
    "            elif board[Rook_pos[0]][i]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "\n",
    "        #down\n",
    "        for i in  range(Rook_pos[0]+1,8):\n",
    "            if board[i][Rook_pos[1]]==\".\":\n",
    "                continue\n",
    "            elif board[i][Rook_pos[1]]==\"B\":\n",
    "                break\n",
    "            elif board[i][Rook_pos[1]]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "        #up\n",
    "        for i in range(Rook_pos[0]-1,-1,-1):\n",
    "            if board[i][Rook_pos[1]]==\".\":\n",
    "                continue\n",
    "            elif board[i][Rook_pos[1]]==\"B\":\n",
    "                break\n",
    "            elif board[i][Rook_pos[1]]==\"p\":\n",
    "                res+=1\n",
    "                break\n",
    "    \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        def count_raw(raw, idx):\n",
    "            count = 0\n",
    "            if idx>0:\n",
    "                for i in range(idx-1,-1,-1):\n",
    "                    if raw[i]=='p':\n",
    "                        if not 'B' in raw[i:idx]:\n",
    "                            count += 1\n",
    "                            break\n",
    "            if idx<7:\n",
    "                for i in range(idx+1,8,1):\n",
    "                    if raw[i]=='p':\n",
    "                        if not 'B' in raw[idx:i]:\n",
    "                            count += 1\n",
    "                            break\n",
    "            return count\n",
    "        raw = len(board)\n",
    "        col = len(board[0])\n",
    "        for i in range(raw):\n",
    "            for j in range(col):\n",
    "                if board[i][j]=='R':\n",
    "                    R = (i,j)\n",
    "                    break\n",
    "        sr = board[R[0]]\n",
    "        sc = [board[i][R[1]] for i in range(raw)]\n",
    "        return count_raw(sr,R[1])+count_raw(sc,R[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n = len(board), len(board[0])\n",
    "        self.ans = 0\n",
    "        # visited = [[0] * n for _ in range(m)]\n",
    "        dirs = [(1,0), (-1,0), (0,1), (0,-1)]\n",
    "        def dfs(board, i, j, dx, dy):\n",
    "            if i < 0 or i >= m or j < 0 or j >= n:\n",
    "                return \n",
    "            if board[i][j] == 'B':\n",
    "                return\n",
    "            if board[i][j] == 'p':\n",
    "                self.ans += 1\n",
    "                return\n",
    "            if board[i][j] == '.':\n",
    "                i += dx\n",
    "                j += dy\n",
    "                dfs(board, i, j, dx, dy)\n",
    "             \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == 'R':\n",
    "                    for d in dirs:\n",
    "                        # next_x = i + d[0]\n",
    "                        # next_y = j + d[1]                \n",
    "                        dfs(board, i + d[0], j + d[1], d[0], d[1])\n",
    "        return self.ans\n",
    "\n",
    "        # count = 0\n",
    "        # for i in range(m):\n",
    "        #     for j in range(n):\n",
    "        #         if board[i][j] == 'R':\n",
    "        #             row = i\n",
    "        #             col = j\n",
    "        # for i in range(row-1, -1, -1):\n",
    "        #     if board[i][col] == 'B':\n",
    "        #         break\n",
    "        #     if board[i][col] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(row+1, m):\n",
    "        #     if board[i][col] == 'B':\n",
    "        #         break\n",
    "        #     if board[i][col] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(col-1, -1, -1):\n",
    "        #     if board[row][i] == 'B':\n",
    "        #         break\n",
    "        #     if board[row][i] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # for i in range(col+1, n):\n",
    "        #     if board[row][i] == 'B':\n",
    "        #         break\n",
    "        #     if board[row][i] == 'p':\n",
    "        #         count += 1\n",
    "        #         break\n",
    "        # return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m = len(board)\n",
    "        if m == 0:\n",
    "            return 0\n",
    "        n = len(board[0])\n",
    "        count = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if board[i][j] == 'R':\n",
    "                    for k in range(i-1,-1,-1):\n",
    "                        if board[k][j] == 'B':\n",
    "                            break\n",
    "                        elif board[k][j] == 'p':\n",
    "                            count += 1\n",
    "                            break\n",
    "                    for k in range(i+1,m):\n",
    "                        if board[k][j] == 'B':\n",
    "                            break\n",
    "                        elif board[k][j] == 'p':\n",
    "                            count += 1\n",
    "                            break\n",
    "                    for k in range(j-1,-1,-1):\n",
    "                        if board[i][k] == 'B':\n",
    "                            break\n",
    "                        elif board[i][k] == 'p':\n",
    "                            count += 1\n",
    "                            break\n",
    "                    for k in range(j+1,n):\n",
    "                        if board[i][k] == 'B':\n",
    "                            break\n",
    "                        elif board[i][k] == 'p':\n",
    "                            count += 1\n",
    "                            break\n",
    "                    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        cnt, st, ed = 0, 0, 0\n",
    "        direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]\n",
    "        \n",
    "        for i in range(len(board)):\n",
    "            for j in range(len(board)):\n",
    "                if board[i][j] == \"R\":\n",
    "                    st, ed = i, j\n",
    "        for i in range(len(direction)):\n",
    "            step = 0\n",
    "            while True:\n",
    "                tx = st + step * direction[i][0]\n",
    "                ty = ed + step * direction[i][1]\n",
    "                if tx < 0 or tx >= 8 or ty < 0 or ty >= 8 or board[tx][ty] == \"B\":\n",
    "                    break\n",
    "                if board[tx][ty] == \"p\":\n",
    "                    cnt += 1\n",
    "                    break\n",
    "                step += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for i in range(8):\n",
    "            for j in range(8):\n",
    "                if board[i][j] == 'R':\n",
    "                    cur = [i, j]\n",
    "                    break\n",
    "        count = 0\n",
    "        for i in range(cur[0]-1, -1, -1):\n",
    "            if board[i][cur[1]] != '.':\n",
    "                if board[i][cur[1]] == 'p':\n",
    "                    count += 1\n",
    "                break\n",
    "        for i in range(cur[0]+1, 8):\n",
    "            if board[i][cur[1]] != '.':\n",
    "                if board[i][cur[1]] == 'p':\n",
    "                    count += 1\n",
    "                break\n",
    "        for j in range(cur[1]-1, -1, -1):\n",
    "            if board[cur[0]][j] != '.':\n",
    "                if board[cur[0]][j] == 'p':\n",
    "                    count += 1\n",
    "                break\n",
    "        for j in range(cur[1]+1, 8):\n",
    "            if board[cur[0]][j] != '.':\n",
    "                if board[cur[0]][j] == 'p':\n",
    "                    count += 1\n",
    "                break\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numRookCaptures(self, board: list[list[str]]) -> int:\r\n",
    "        Rook_list = list()\r\n",
    "        Bishop_list = list()\r\n",
    "        pawn_list = list()\r\n",
    "        for line in range(0, 8):\r\n",
    "            for col in range(0, 8):\r\n",
    "                if board[line][col] == 'R':\r\n",
    "                    Rook_list = [line, col] \r\n",
    "                    break\r\n",
    "        # 棋子数\r\n",
    "        res = 0\r\n",
    "        # 向上遍历\r\n",
    "        i = Rook_list[0]\r\n",
    "        while i >= 0:\r\n",
    "            if board[i][Rook_list[1]] == 'B':\r\n",
    "                break\r\n",
    "            elif board[i][Rook_list[1]] == 'p':\r\n",
    "                res += 1\r\n",
    "                break\r\n",
    "            i -= 1\r\n",
    "        # 向下遍历\r\n",
    "        i = Rook_list[0] + 1\r\n",
    "        while i < 8:\r\n",
    "            if board[i][Rook_list[1]] == 'B':\r\n",
    "                break\r\n",
    "            elif board[i][Rook_list[1]] == 'p':\r\n",
    "                res += 1\r\n",
    "                break\r\n",
    "            i += 1\r\n",
    "        # 向左遍历\r\n",
    "        j = Rook_list[1] - 1\r\n",
    "        while j >= 0:\r\n",
    "            if board[Rook_list[0]][j] == 'B':\r\n",
    "                break\r\n",
    "            elif board[Rook_list[0]][j] == 'p':\r\n",
    "                res += 1\r\n",
    "                break\r\n",
    "            j -= 1\r\n",
    "        # 向右遍历\r\n",
    "        j = Rook_list[1] + 1\r\n",
    "        while j < 8:\r\n",
    "            if board[Rook_list[0]][j] == 'B':\r\n",
    "                break\r\n",
    "            elif board[Rook_list[0]][j] == 'p':\r\n",
    "                res += 1\r\n",
    "                break            \r\n",
    "            j += 1\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        Dir = [(0,1),(0,-1),(-1,0),(1,0)]\n",
    "        m = len(board)\n",
    "\n",
    "        def dfs(x,y):\n",
    "            cnt = 0\n",
    "            for a,b in Dir:\n",
    "                x1 = x\n",
    "                y1 = y\n",
    "                while 0<= x1+a <8 and 0<= y1+b<8:\n",
    "                    x1 = x1+a\n",
    "                    y1 = y1+b\n",
    "                    if board[x1][y1] == 'B':\n",
    "                        break\n",
    "                    if board[x1][y1] == 'p':\n",
    "                        cnt += 1\n",
    "                        break\n",
    "                 \n",
    "\n",
    "            return cnt\n",
    "\n",
    "        for i in range(8):\n",
    "            for  j in range(8):\n",
    "                if board[i][j] == \"R\":\n",
    "                    return dfs(i,j)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        ans = 0\n",
    "        \n",
    "        def getpos():\n",
    "            for i in range(8):\n",
    "                for j in range(8):\n",
    "                    if board[i][j] == 'R':\n",
    "                        return i,j\n",
    "        \n",
    "        r,c = getpos()\n",
    "        for i in range(r,-1,-1):\n",
    "            if board[i][c] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "            if board[i][c] == 'B':\n",
    "                break\n",
    "        for i in range(r,8):\n",
    "            if board[i][c] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "            if board[i][c] == 'B':\n",
    "                break\n",
    "        for i in range(c,-1,-1):\n",
    "            if board[r][i] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "            if board[r][i] == 'B':\n",
    "                break\n",
    "        for i in range(c,8):\n",
    "            if board[r][i] == 'p':\n",
    "                ans += 1\n",
    "                break\n",
    "            if board[r][i] == 'B':\n",
    "                break\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        for row in range(0,8):\n",
    "            if \"R\" in board[row]:\n",
    "                rook = [row, board[row].index('R')]\n",
    "        eat = 0\n",
    "        l = rook[1] - 1\n",
    "        r = rook[1] + 1\n",
    "        u = rook[0] + 1\n",
    "        d = rook[0] - 1\n",
    "        while l >= 0:\n",
    "            if board[rook[0]][l] == 'p':\n",
    "                eat += 1\n",
    "                break\n",
    "            elif board[rook[0]][l] == 'B':\n",
    "                break\n",
    "            l -= 1\n",
    "        while r < 8:\n",
    "            if board[rook[0]][r] == 'p':\n",
    "                eat += 1\n",
    "                break\n",
    "            elif board[rook[0]][r] == 'B':\n",
    "                break\n",
    "            r += 1\n",
    "        while u < 8:\n",
    "            if board[u][rook[1]] == 'p':\n",
    "                eat += 1\n",
    "                break\n",
    "            elif board[u][rook[1]] == 'B':\n",
    "                break\n",
    "            u += 1\n",
    "        while d >= 0:\n",
    "            if board[d][rook[1]] == 'p':\n",
    "                eat += 1\n",
    "                break\n",
    "            elif board[d][rook[1]] == 'B':\n",
    "                break\n",
    "            d -= 1\n",
    "        return eat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        #一，捕获车的位置\n",
    "        h,w = len(board),len(board[0])\n",
    "        num = 0\n",
    "        car_h = 0\n",
    "        car_w = 0\n",
    "        for i in range(h):\n",
    "            for j in range(w):\n",
    "                if board[i][j] == 'R':\n",
    "                    car_h = i\n",
    "                    car_w = j\n",
    "                    break\n",
    "        \n",
    "        #二，上下左右以此判断是否有卒\n",
    "        for i in range(car_w,w):\n",
    "            if board[car_h][i] == 'B':\n",
    "                break\n",
    "            elif board[car_h][i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        for i in range(car_w,-1,-1):\n",
    "            if board[car_h][i] == 'B':\n",
    "                break\n",
    "            elif board[car_h][i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        for i in range(car_h, -1, -1):\n",
    "            if board[i][car_w] == 'B':\n",
    "                break\n",
    "            elif board[i][car_w] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        for i in range(car_h,h):\n",
    "            if board[i][car_w] == 'B':\n",
    "                break\n",
    "            elif board[i][car_w] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        m, n, num = 0, 0, 0\n",
    "        for i, j in itertools.product(range(8), range(8)):\n",
    "            if board[i][j] == 'R':\n",
    "                m, n = i, j\n",
    "                break\n",
    "        for i in range(m, len(board)):\n",
    "            if board[m][i] == 'B':\n",
    "                break\n",
    "            elif board[m][i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        # 左\n",
    "        for i in range(m, -1, - 1):\n",
    "            if board[m][i] == 'B':\n",
    "                break\n",
    "            elif board[m][i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        # 获取y轴\n",
    "        ans = []\n",
    "        for j in range(8):\n",
    "            for i in range(len(board)):\n",
    "                if j == n:\n",
    "                    ans.append(board[i][j])\n",
    "        # 上\n",
    "        for i in range(n, -1, -1):\n",
    "            if ans[i] == 'B':\n",
    "                break\n",
    "            elif ans[i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        # 下\n",
    "        for i in range(n, 8):\n",
    "            if ans[i] == 'B':\n",
    "                break\n",
    "            elif ans[i] == 'p':\n",
    "                num = num + 1\n",
    "                break\n",
    "        return num\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numRookCaptures(self, board: List[List[str]]) -> int:\n",
    "        line = list(filter(lambda x:\"R\" in x,board))[0]\n",
    "        row = [lst[line.index(\"R\")]  for lst in board]\n",
    "        newline = list(filter(lambda x:x!='.',line))\n",
    "        newrow = list(filter(lambda x:x!='.',row))\n",
    "        res = 0\n",
    "        a = newline.index(\"R\")\n",
    "        if len(newline) > 1:\n",
    "            if a == 0:\n",
    "                if newline[1] == \"p\":\n",
    "                    res += 1\n",
    "            elif a == len(newline)-1:\n",
    "                if newline[-2] == \"p\":\n",
    "                    res += 1\n",
    "            else:\n",
    "                if newline[a+1] == \"p\":\n",
    "                    res += 1\n",
    "                if newline[a-1] == \"p\":\n",
    "                    res += 1\n",
    "        b = newrow.index(\"R\")\n",
    "        if len(newrow) > 1:\n",
    "            if b == 0:\n",
    "                if newrow[1] == \"p\":\n",
    "                    res += 1\n",
    "            elif b == len(newrow)-1:\n",
    "                if newrow[-2] == \"p\":\n",
    "                    res += 1\n",
    "            else:\n",
    "                if newrow[b+1] == \"p\":\n",
    "                    res += 1\n",
    "                if newrow[b-1] == \"p\":\n",
    "                    res += 1\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
